<template>
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu
          mode="inline"
          v-model:selectedKeys="selectedKeys2"
          v-model:openKeys="openKeys"
          :style="{ height: '100%', borderRight: 0 }"
          @click= "handleClick"
      >
        <a-menu-item key="welcome">
            <MailOutlined />
            <span>欢迎</span>
        </a-menu-item>
        <a-sub-menu v-for="item in level1" :key="item.id">
          <template v-slot:title>
            <span><user-outlined />{{item.name}}</span>
          </template>
          <a-menu-item v-for="child in item.children" :key="child.id">
            <MailOutlined /><span>{{child.name}}</span>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <div class="welcome" v-show="isShowWelcome">
        <h1>欢迎使用本知识库</h1>
      </div>
      <a-list v-show="!isShowWelcome" item-layout="vertical" size="large" :grid="{gutter:20, column:3 }" :data-source="ebooks">
        <template #renderItem="{ item }">
          <a-list-item key="item.title">
            <template #actions>
            <span>
                <component v-bind:is="'FileOutlined'" style="margin-right: 8px"/>
                {{ item.docCount }}
              </span>
              <span>
                <component v-bind:is="'UserOutlined'" style="margin-right: 8px"/>
                {{ item.viewCount }}
              </span>
              <span>
                <component v-bind:is="'LikeOutlined'" style="margin-right: 8px"/>
                {{ item.voteCount }}
              </span>
            </template>
            <a-list-item-meta :description="item.description">
              <template #title>
                <router-link :to="'/doc?ebookId=' + item.id">
                  {{ item.name }}
                </router-link>
              </template>
              <template #avatar><a-avatar :src="item.cover" shape="square" :size="50"/></template>
            </a-list-item-meta>
          </a-list-item>
        </template>
      </a-list>
    </a-layout-content>
  </a-layout>
</template>

<script lang="ts">
import { defineComponent,onMounted,ref} from 'vue';
import axios from 'axios';
import { message } from 'ant-design-vue'
import {Tool} from "@/util/tool"


export default defineComponent({
  name: 'Home',
  setup(){
    const ebooks = ref();//ref绑定响应式数据
    // const ebooks1 = reactive({books: []});//使用reactive绑定响应式数据

    const level1 = ref(); // 一级分类树，children属性就是二级分类
    let categorys: any;
    /**
     * 查询所有分类
     **/
    const handleQueryCategory = () => {
      // 如果不清空现有数据，则编辑保存重新加载数据后，再点编辑，则列表显示的还是编辑前的数据
      // categorys.value = [];
      axios.get("/category/all").then((response) => {
        const data = response.data;
        if (data.success) {
          categorys = data.content;
          console.log("原始数组：", categorys);

          level1.value = [];
          level1.value = Tool.array2Tree(categorys, 0);
          console.log("树形结构：", level1.value);
        }else {
          message.error(data.message);
        }
      });
    };

    const handleQueryEbook = ()=>{
      axios.get( "/ebook/list",{
        params: {
          page: 1,
          size: 1000,
          category2Id: category2Id
        }
      }).then((response)=>{
        const data = response.data;
        ebooks.value = data.content.list;//使用ref需要ref().value
        // ebooks1.books = data.content;//使用reactive需要指定字段
      })
    }

    const isShowWelcome = ref(true);
    let category2Id = 0;
    const handleClick = (value: any) => {
      // console.log("menu click",value)
      if (value.key === 'welcome'){
        isShowWelcome.value = true;
      }else{
        isShowWelcome.value = false;
        category2Id = value.key;
        handleQueryEbook();
      }
      // isShowWelcome.value = value.key === 'welcome';

    }

    onMounted(()=>{
      handleQueryCategory();
    })

    return{
      ebooks,
      // ebooks2: toRef(ebooks1,"books"),
      // listData,
      pagination: {
        onChange: (page: any) => {
          console.log(page);
        },
        pageSize: 3,
      },
    //   actions: [
    //   { type: 'StarOutlined', text: '156' },
    //   { type: 'LikeOutlined', text: '156' },
    //   { type: 'MessageOutlined', text: '2' },
    // ],

      handleClick,
      level1,
      isShowWelcome,
    }
  }
});
</script>
